package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.PageResult;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.mapper.TbItemParamMapper;
import com.usian.pojo.TbItemParam;
import com.usian.pojo.TbItemParamExample;
import com.usian.pojo.TbItemParamItem;
import com.usian.pojo.TbItemParamItemExample;
import com.usian.redis.RedisClient;
import com.usian.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Value("${ITEM_INFO}")
    private String ITEM_INFO;

    @Value("${PARAM}")
    private String PARAM;

    @Value("${ITEM_INFO_EXPIRE}")
    private Integer ITEM_INFO_EXPIRE;

    @Autowired
    private RedisClient redisClient;

    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {

        TbItemParamExample tbItemParamExample =new TbItemParamExample();
        TbItemParamExample.Criteria criteria = tbItemParamExample.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> paramList = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);

        if(paramList.size()>0){
            return paramList.get(0);
        }
        return null;
    }

    @Override
    public PageResult selectItemParamAll(Integer page, Integer rows) {

        PageHelper.startPage(page,rows);

        TbItemParamExample example =new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("updated desc");
        List<TbItemParam> list = tbItemParamMapper.selectByExampleWithBLOBs(example);

        PageInfo<TbItemParam> pageInfo =new PageInfo<>(list);

        PageResult pageResult =new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(pageInfo.getList());
        return pageResult;
    }

    @Override
    public int insertItemParam(Long itemCatId, String paramData) {
        TbItemParamExample example =new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> list = tbItemParamMapper.selectByExample(example);
        if(list.size()>0){
            return 0;
        }

        TbItemParam tbItemParam =new TbItemParam();
        tbItemParam.setItemCatId(itemCatId);
        tbItemParam.setParamData(paramData);
        tbItemParam.setCreated(new Date());
        tbItemParam.setUpdated(new Date());

        int result = tbItemParamMapper.insertSelective(tbItemParam);
        return result;
    }

    @Override
    public int deleteItemParamById(Long id) {
        int result = tbItemParamMapper.deleteByPrimaryKey(id);
        return result;
    }

    @Override
    public TbItemParamItem selectTbItemParamItemByItemId(Long itemId) {

        TbItemParamItem itemParamItem=(TbItemParamItem)redisClient.get(ITEM_INFO + ":" + itemId + ":"+PARAM);
        if(itemParamItem!=null){
            return itemParamItem;
        }

        TbItemParamItemExample example =new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(example);

        if(list.size()>0){

            if(redisClient.setnx(ITEM_INFO + ":" + itemId + ":"+PARAM+":nx",333,30)){
                if(list.get(0)==null){
                    redisClient.set(ITEM_INFO + ":" + itemId + ":"+PARAM,new TbItemParamItem(1L,"redis空值"));
                    redisClient.expire(ITEM_INFO + ":" + itemId + ":"+PARAM,30);
                }else{
                    redisClient.set(ITEM_INFO + ":" + itemId + ":"+PARAM,list.get(0));
                    redisClient.expire(ITEM_INFO + ":" + itemId + ":"+PARAM, -1);
                }
                redisClient.del(ITEM_INFO + ":" + itemId + ":"+PARAM+":nx");
            }
            return list.get(0);
        }
        return null;
    }


    public static void main(String[] args) {
        Random random = new Random();//默认构造方法
        int i2 = random.nextInt(100);
        System.out.println(i2);
    }
}
